//
//  SLHisGoodsViewController.m
//  StarLight
//
//  Created by Duke.wu on 16/5/19.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLHisGoodsViewController.h"
#import "SLNearlyCollectionViewCell.h"
#import "SLNearlyProductDetailViewModel.h"
#import "SLProductDetailViewController.h"
#import "SLFriendManager.h"
#import "SLFriendAddViewController.h"
#import "STPopup.h"
#import "SLShipOwnerVipLevelView.h"
#import "SLDivideView.h"



@interface SLHisGoodsViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) SLNearlyProductDetailViewModel *goodsVM;

@property (nonatomic, strong) NSString *hid;

@property (nonatomic, strong) UICollectionView *goodsCollectionView;

@property (nonatomic, strong) UIView *headerView;

@property (nonatomic ,strong) UILabel *nameLB;

@property (nonatomic ,strong) UIImageView *goodsOwnerIcon;

@property (nonatomic, strong) UILabel *typeLabel;

@property (nonatomic ,strong) UIImageView *arrowheadIcon;

@property (nonatomic, strong) UIButton *buddyBtn;

@property (nonatomic, strong) UIButton *tipBtn;

@property (nonatomic, strong) SLFriendManager *friendMg;

@property (nonatomic, strong) UILabel *vipLabel;

@property (nonatomic, strong) SLShipOwnerVipLevelView *levelView;

@property (nonatomic, strong) UILabel *activityLabel;

@property (nonatomic, strong) SLDivideView *divide1;

@property (nonatomic, strong) UILabel *popularityLabel;

@property (nonatomic, strong) UILabel *popularityNum;

@property (nonatomic, strong) SLDivideView *divide2;

@property (nonatomic, strong) UILabel *hisLabel;

@property (nonatomic, strong) UILabel *hisNum;

@property (nonatomic, strong) SLDivideView *divide3;

@property (nonatomic, strong) UILabel *attentionLabel;

@property (nonatomic, strong) UILabel *attentionNum;

@end

@implementation SLHisGoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  [self setUpUI];
  [self addConstrains];
  [self addObservers];
}

- (void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  [self.navigationController setNavigationBarHidden:NO animated:YES];
  self.view.backgroundColor = [UIColor defeutBackgroudColor];
  self.title = @"他的货源";
}


#pragma mark - Private Methods

- (void)addObservers{
  @weakify(self)
 
  [[RACObserve(self.goodsVM, moreModel) filter:^BOOL(id value) {
    return value != nil;
  }] subscribeNext:^(SLMoreGoodsModel *model) {
    
    @strongify(self)
    [self changeStatusWithState:model.buddyState];
    self.nameLB.text = model.name;
    [self.goodsOwnerIcon sd_setImageWithURL:[NSURL URLWithString:model.headPhoto]
                           placeholderImage:[UIImage imageNamed:@"head"]];
    [self setUpVip:model.role];
    [self.goodsCollectionView reloadData];
  }];
  
  [[[NSNotificationCenter defaultCenter] rac_addObserverForName:kAddFriendSuccess object:nil] subscribeNext:^(id x) {
    @strongify(self)
    [self restart];
  }];
}


// 显示操作按钮
static BOOL rightItemSelected = YES;
- (void)showOper{
  rightItemSelected = !rightItemSelected;
  self.tipBtn.hidden = rightItemSelected;
  self.buddyBtn.hidden = rightItemSelected;
  
}


#pragma mark - Public Methods

- (void)setUpVip:(NSInteger)role{
  if (role == 0) {
    [self.levelView removeFromSuperview];
    self.vipLabel.text = @"普通用户";
    self.vipLabel.textColor = [UIColor blackColor];
  }else if(role > 0){
    [self.levelView configWithLevel:[NSString stringWithFormat:@"%ld",role]];
  }
}

- (void)changeHid:(NSString *)newHid{
  _hid = newHid;
  self.goodsVM = [[SLNearlyProductDetailViewModel alloc] initWithHid:newHid];
}

- (void)changeStatusWithState:(NSInteger)state{
  
  if (state != 3) {
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"取消关注" style:UIBarButtonItemStylePlain target:self action:@selector(showOper)];
    self.navigationItem.rightBarButtonItem = rightItem;
  };
  
  self.buddyBtn.enabled = YES;
  switch (state) {
    case -1:{
      [self.buddyBtn setTitle:@"取消屏蔽" forState:UIControlStateNormal];
      self.buddyBtn.backgroundColor = SLBlackColor;
    }
      break;
    case 0:{
      [self.buddyBtn setTitle:@" 添加 " forState:UIControlStateNormal];
      self.buddyBtn.backgroundColor = SLBlackColor;
    }
      break;
    case 1:{
      [self.buddyBtn setTitle:@" 已添加 " forState:UIControlStateNormal];
      self.buddyBtn.backgroundColor = [UIColor grayColor];
      self.buddyBtn.enabled = NO;
    }
      break;
    case 2:{
      [self.buddyBtn setTitle:@" 接受 " forState:UIControlStateNormal];
      self.buddyBtn.backgroundColor = SLBlackColor;
    }
      break;
    case 3:{
      [self.buddyBtn removeFromSuperview];
      [self.tipBtn removeFromSuperview];
    }
      break;
    case 4:{
      [self.buddyBtn removeFromSuperview];
    }
      break;
      
    default:
      break;
  }
  
}

- (void)didTapedoperFriendBtn{
  
  
  @weakify(self)
  SLMoreGoodsModel *model = self.goodsVM.moreModel;
  
  switch (model.buddyState) {
    case -1:{
      // 取消屏蔽
      [self.friendMg operFriendWithHuid:model.hid opers:@"3" block:^{
        @strongify(self)
        [self restart];
      }];
    }
      break;
    case 0:{
      // 添加
      SLFriendAddViewController *addFVC = [SLFriendAddViewController new];
      addFVC.goodsMoreModel = model;
      STPopupController *popVC = [[STPopupController alloc] initWithRootViewController:addFVC];
      popVC.navigationBarHidden = YES;
      popVC.cornerRadius = 5;
      [popVC presentInViewController:self];
    }
      break;
    case 2:{
      // 接受
      [self.friendMg operFriendWithHuid:model.hid opers:@"1" block:^{
        @strongify(self)
        [self restart];
      }];
    }
      break;
    default:
      break;
  }
  
  
}

- (void)restart{
  [self.goodsVM refreshWith:self.hid];
}

- (void)screenFriend{
  @weakify(self)
  SLMoreGoodsModel *model = self.goodsVM.moreModel;
  [self.friendMg operFriendWithHuid:model.hid opers:@"2" block:^{
    @strongify(self)
    [self restart];
  }];

}



#pragma mark - UI

- (void)addConstrains{
  
  [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.mas_topLayoutGuideBottom).offset(0);
    make.width.equalTo(self.view);
    make.centerX.equalTo(self.view);
    make.height.equalTo(@(130*MIX_H));
  }];
  
  [self.goodsOwnerIcon mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.headerView).offset(10);
    make.left.equalTo(self.headerView).offset(18);
    make.size.mas_equalTo(CGSizeMake(50*MIX_W, 50*MIX_W));
  }];
  
  [self.nameLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.goodsOwnerIcon.mas_right).offset(15);
    make.top.equalTo(self.goodsOwnerIcon.mas_top).offset(3);
  }];
  
  [self.arrowheadIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.headerView).offset(-20);
        make.center.equalTo(self.nameLB);
        make.size.mas_equalTo(CGSizeMake(7*MIX_W, 11*MIX_W));
  }];
  
  [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.arrowheadIcon).offset(-45);
    make.centerY.equalTo(self.nameLB);
  }];
  
  [self.vipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.goodsOwnerIcon.mas_right).offset(15);
      make.bottom.equalTo(self.goodsOwnerIcon.mas_bottom);
  }];
  
  [self.levelView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(self.vipLabel);
    make.left.equalTo(self.vipLabel.mas_right).offset(5);
    make.size.mas_equalTo(CGSizeMake(20, 20));
  }];
  
    [self.activityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.headerView).offset(-20);
        make.centerY.equalTo(self.vipLabel);
    }];

    [self.divide1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.goodsOwnerIcon.mas_bottom).offset(20);
        make.left.equalTo(self.headerView);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 1));
    }];
    
    [self.popularityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.divide1).offset(12);
        make.left.equalTo(self.headerView).offset((SCREEN_WIDTH / 6)-16);
    }];
    
    [self.popularityNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.popularityLabel.mas_bottom).offset(5);
        make.centerX.equalTo(self.popularityLabel);
    }];
    
    [self.divide2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.divide1.mas_bottom);
        make.left.equalTo(self.headerView).offset(SCREEN_WIDTH/3);
        make.size.mas_equalTo(CGSizeMake(1, 63));
    }];
    
    [self.hisLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.popularityLabel.mas_top);
        make.left.equalTo(self.headerView).offset((SCREEN_WIDTH / 2)-32);
    }];
    
    [self.hisNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.popularityNum.mas_top);
        make.centerX.equalTo(self.hisLabel);
    }];
    
    [self.divide3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.divide1.mas_bottom);
        make.right.equalTo(self.headerView).offset(-SCREEN_WIDTH/3);
        make.size.mas_equalTo(CGSizeMake(1, 63));
    }];
    
    [self.attentionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.popularityLabel.mas_top);
        make.right.equalTo(self.headerView).offset(-(SCREEN_WIDTH / 6)+32);
    }];
    
    [self.attentionNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.popularityNum.mas_top);
        make.centerX.equalTo(self.attentionLabel);
    }];
    
  [self.tipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self.view);
    make.top.equalTo(self.mas_topLayoutGuideBottom);
    make.size.mas_equalTo(CGSizeMake(50*MIX_W, 30*MIX_W));
  }];
  
   
  [self.buddyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.size.right.equalTo(self.tipBtn);
    make.top.equalTo(self.tipBtn.mas_bottom);
  }];
  
  [self.goodsCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.width.equalTo(self.view);
    make.bottom.equalTo(self.view);
    make.top.equalTo(self.headerView.mas_bottom).offset(10);
  }];
    
}

- (void)setUpUI{
  [self.view addSubview:self.headerView];
  [self.view addSubview:self.goodsCollectionView];
  [self.headerView addSubview:self.goodsOwnerIcon];
  [self.headerView addSubview:self.nameLB];
  [self.headerView addSubview:self.arrowheadIcon];
  [self.headerView addSubview:self.typeLabel];
  [self.headerView addSubview:self.vipLabel];
  [self.headerView addSubview:self.levelView];
  [self.headerView addSubview:self.activityLabel];
    [self.headerView addSubview:self.divide1];
    [self.headerView addSubview:self.popularityLabel];
    [self.headerView addSubview:self.popularityNum];
    [self.headerView addSubview:self.divide2];
    [self.headerView addSubview:self.hisLabel];
    [self.headerView addSubview:self.hisNum];
    [self.headerView addSubview:self.divide3];
    [self.headerView addSubview:self.attentionLabel];
    [self.headerView addSubview:self.attentionNum];
  [self.view addSubview:self.buddyBtn];
  [self.view addSubview:self.tipBtn];
  
  
}





- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
  if (!self.reach.isReachable) {
    return [[NSAttributedString alloc] initWithString:@"无网络连接, 请连接网络后刷新" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
  }
  return [[NSAttributedString alloc] initWithString:@"暂无货源" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  return self.goodsVM.moreModel.goods.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  SLNearlyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  [cell configWithType:NearlyCellTypeShip];
  return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(SLNearlyCollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
  [cell configNearlyCellWithProductModel:self.goodsVM.moreModel.goods[indexPath.row]];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  SLNearLyProduct *model = self.goodsVM.moreModel.goods[indexPath.row];
  SLProductDetailViewController *goodsDetailVC = [SLProductDetailViewController new];
  goodsDetailVC.isNextAvailable = NO;
  [goodsDetailVC changeGid:model.gid];
  
  [self.navigationController pushViewController:goodsDetailVC animated:YES];
}




- (UICollectionView *)goodsCollectionView{
  if (!_goodsCollectionView) {
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH-20, 140);
    _goodsCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _goodsCollectionView.backgroundColor = [UIColor defeutBackgroudColor];
    [_goodsCollectionView registerClass:[SLNearlyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    _goodsCollectionView.delegate = self;
    _goodsCollectionView.dataSource = self;
    _goodsCollectionView.emptyDataSetSource = self;
  }
  return _goodsCollectionView;
}


- (UILabel *)nameLB{
  if (!_nameLB) {
    _nameLB = [UILabel labelwithBoldFontSize:16 textColor:SLBlackColor text:@""];
  }
  return _nameLB;
}

- (UILabel *)typeLabel{
  if (!_typeLabel) {
    _typeLabel = [UILabel labelWithFontSize:14 textColor:SLInkColor text:@"张三"];
  }
  return _typeLabel;
}

- (UILabel *)activityLabel
{
    if (!_activityLabel) {
        _activityLabel = [UILabel labelwithBoldFontSize:14 textColor:SLInkColor text:@"活跃度: 20"];
    }
    return _activityLabel;
}

- (UIImageView *)goodsOwnerIcon{
  if (!_goodsOwnerIcon) {
    _goodsOwnerIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"head"]];
    _goodsOwnerIcon.backgroundColor = SLYellowColor;
  }
  return _goodsOwnerIcon;
}

- (UIImageView *)arrowheadIcon
{
    if (!_arrowheadIcon) {
        _arrowheadIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ic_arrow_right"]];
    }
    return _arrowheadIcon;
}

- (UIView *)headerView{
  if (!_headerView) {
    _headerView = [UIView new];
    _headerView.backgroundColor = [UIColor whiteColor];
  }
  return _headerView;
}


- (UIButton *)buddyBtn{
  if (!_buddyBtn) {
    _buddyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _buddyBtn.backgroundColor = SLBlackColor;
    _buddyBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [_buddyBtn setTitleColor:SLYellowColor forState:UIControlStateNormal];
    [_buddyBtn addTarget:self action:@selector(didTapedoperFriendBtn) forControlEvents:UIControlEventTouchUpInside];
    _buddyBtn.hidden = YES;
  }
  return _buddyBtn;
}

- (UIButton *)tipBtn{
  if (!_tipBtn) {
    _tipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_tipBtn setTitle:@" 屏蔽 " forState: UIControlStateNormal];
    [_tipBtn setTitleColor:SLYellowColor forState:UIControlStateNormal];
    [_tipBtn addTarget:self action:@selector(screenFriend) forControlEvents:UIControlEventTouchUpInside];
    _tipBtn.backgroundColor = SLBlackColor;
    _tipBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    _tipBtn.hidden = YES;
  }
  return _tipBtn;
}


- (SLFriendManager *)friendMg{
  if (!_friendMg) {
    _friendMg = [SLFriendManager new];
  }
  return _friendMg;
}

- (UILabel *)popularityLabel
{
    if (!_popularityLabel) {
        _popularityLabel = [UILabel labelwithBoldFontSize:16 textColor:SLInkColor text:@"人气"];
    }
    return _popularityLabel;
}

- (UILabel *)popularityNum
{
    if (!_popularityNum) {
        _popularityNum = [UILabel labelwithBoldFontSize:16 textColor:SLInkColor text:@"20"];
    }
    return _popularityNum;
}


- (UILabel *)hisLabel
{
    if (!_hisLabel) {
        _hisLabel = [UILabel labelwithBoldFontSize:16 textColor:SLInkColor text:@"关注他的"];
    }
    return _hisLabel;
}

- (UILabel *)hisNum
{
    if (!_hisNum) {
        _hisNum = [UILabel labelwithBoldFontSize:16 textColor:SLInkColor text:@"28"];
    }
    return _hisNum;
}

- (UILabel *)attentionLabel
{
    if (!_attentionLabel) {
        _attentionLabel = [UILabel labelwithBoldFontSize:16 textColor:SLInkColor text:@"他关注的"];
    }
    return _attentionLabel;
}

- (UILabel *)attentionNum
{
    if (!_attentionNum) {
        _attentionNum = [UILabel labelwithBoldFontSize:16 textColor:SLInkColor text:@"43"];
    }
    return _attentionNum;
}

-(UILabel *)vipLabel{
  if (!_vipLabel) {
    _vipLabel = [UILabel labelwithBoldFontSize:15 textColor:[UIColor redColor] text:@"VIP"];
  }
  return _vipLabel;
}

- (SLDivideView *)divide1
{
    if (!_divide1) {
        _divide1 = [SLDivideView new];
    }
    return _divide1;
}

- (SLDivideView *)divide2
{
    if (!_divide2) {
        _divide2 = [SLDivideView new];
    }
    return _divide2;
}

- (SLDivideView *)divide3
{
    if (!_divide3) {
        _divide3 = [SLDivideView new];
    }
    return _divide3;
}

- (SLShipOwnerVipLevelView *)levelView{
  if (!_levelView) {
    _levelView = [SLShipOwnerVipLevelView new];
  }
  return _levelView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
